package com.littleHan.brushQuestions.exception.handler;

import com.alibaba.fastjson.JSON;
import com.littleHan.brushQuestions.common.Result;
import com.littleHan.brushQuestions.exception.*;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(FailedToLoadException.class)
    @ResponseBody
    public String failedToLoadException(FailedToLoadException e){
        return JSON.toJSONString(new Result().setCode(500).setMessage(e.getMessage()));
    }

    @ExceptionHandler(FailedToInsertException.class)
    @ResponseBody
    public String failedToInsertException(FailedToInsertException e){
        return JSON.toJSONString(new Result().setCode(500).setMessage(e.getMessage()));
    }

    @ExceptionHandler(FailedToUpdateException.class)
    @ResponseBody
    public String failedToUpdateException(FailedToUpdateException e){
        return JSON.toJSONString(new Result().setCode(500).setMessage(e.getMessage()));
    }

    @ExceptionHandler(FailedToDeleteException.class)
    @ResponseBody
    public String failedToDeleteException(FailedToDeleteException e){
        return JSON.toJSONString(new Result().setCode(500).setMessage(e.getMessage()));
    }

    @ExceptionHandler(FailedToUploadException.class)
    @ResponseBody
    public String failedToUploadException(FailedToUploadException e) {
        return JSON.toJSONString(new Result().setCode(500).setMessage(e.getMessage()));
    }
}
